<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//jquery+php 鼠标拖动裁切图片
/*
1) 加载JS(jquery)
<script type="text/javascript" src="<?=gurl('js');?>/jquery.imgareaselect.min.js"></script>

2) library
$this->jqphoto->upload($_FILES['image']);
$this->jqphoto->upload_thumb($_POST);

*/

class CI_Jqphoto
{
	function CI_Jqphoto($params = array())
	{
		session_start(); 
		
		if (!isset($_SESSION['random_key']) || strlen($_SESSION['random_key'])==0){
			$_SESSION['random_key'] = strtotime(date('Y-m-d H:i:s'));
			$_SESSION['user_file_ext']= "";
		}
		
		$this->image_ext = $this->error = "";
		$this->upload_dir = "./data/upload_pic"; 			// 存放上传图片的目录
		$this->upload_path = $this->upload_dir."/";				// 存放裁切后的图像
		$this->large_image_prefix = "resize_"; 			// 上传后重命名未裁切图片的附加字符
		$this->thumb_image_prefix = "thumbnail_";			// 上传后重命名已裁切图片的附加字符
		$this->large_image_name = $this->large_image_prefix.$_SESSION['random_key'];     // 上传后未裁切的图片名的命名规则
		$this->thumb_image_name = $this->thumb_image_prefix.$_SESSION['random_key'];     // 上传后已裁切的图片名的命名规则
		$this->max_file = "5"; 							// 上传图片大小，这里默认5M
		$this->max_width = "5000";							// 上传图片的最大宽度
		// 以下数组存放允许上传的文件类型
		$this->allowed_image_types = array('image/pjpeg'=>"jpg",'image/jpeg'=>"jpg",'image/jpg'=>"jpg",'image/png'=>"png",'image/x-png'=>"png",'image/gif'=>"gif");
		$this->allowed_image_ext = array_unique($this->allowed_image_types); 
		foreach ($this->allowed_image_ext as $mime_type => $ext) {
			$this->image_ext.= strtoupper($ext)." ";
		}
		$this->large_image_location = $this->upload_path.$this->large_image_name.$_SESSION['user_file_ext'];
		$this->thumb_image_location = $this->upload_path.$this->thumb_image_name.$_SESSION['user_file_ext'];
		$this->large_photo_exists = $this->thumb_photo_exists = "";
		$this->thumb_width = "60";
		$this->thumb_height = "60";
	}
	
	//执行大图上传
	function upload($image) {
		
		$userfile_name = $image['name'];
		$userfile_tmp = $image['tmp_name'];
		$this->userfile_size = $image['size'];
		$this->userfile_type = $image['type'];
		$filename = basename($image['name']);
		$this->file_ext = strtolower(substr($filename, strrpos($filename, '.') + 1));
		$error = $this->check_upload($image);
		
		if (strlen($error)==0){
					
			if (isset($image['name'])){
				$this->large_image_location = $this->upload_path.$this->large_image_name.".".$this->file_ext;
				$this->thumb_image_location = $this->upload_path.$this->thumb_image_name.".".$this->file_ext;
				$_SESSION['user_file_ext']=".".$this->file_ext;
				
				move_uploaded_file($userfile_tmp, $this->large_image_location);
				chmod($this->large_image_location, 0777);

				$width = $this->getWidth($this->large_image_location);
				$height = $this->getHeight($this->large_image_location);
				if ($width > $this->max_width){
					$scale = $this->max_width/$width;
					$uploaded = $this->resizeImage($this->large_image_location,$width,$height,$scale);
				}else{
					$scale = 1;
					$uploaded = $this->resizeImage($this->large_image_location,$width,$height,$scale);
				}
				if (file_exists($this->thumb_image_location)) {
					//unlink($this->thumb_image_location);
				}
			}
		}
		
		//裁剪大图片
		if(file_exists('./data/upload_pic/'.$this->large_image_name.$_SESSION['user_file_ext'])) {
			$CI =& get_instance();
			$CI->load->library("imagecrop");//加载library
			$params = array('src_file' => './data/upload_pic/'.$this->large_image_name.$_SESSION['user_file_ext'], 'dst_file' => './data/upload_pic/'.$this->large_image_name.$_SESSION['user_file_ext']);
			$CI->imagecrop->src_file=$params['src_file'];
			$CI->imagecrop->dst_file=$params['dst_file'];
			$CI->imagecrop->LoadImage();
			$CI->imagecrop->Crop(300,300,1);
			$CI->imagecrop->SaveImage();
			$CI->imagecrop->destory();
		}
		
		//1) 大图片地址
		$this->large_photo_exists = "<img src=\"".gurl("data")."/upload_pic/".$this->large_image_name.$_SESSION['user_file_ext']."\" alt=\"大图\"/>";
		return $this->large_photo_exists;
	
	} 
	
	//裁切图片上传处理
	function upload_thumb($post) {
		$x1 = $post["x1"];
		$y1 = $post["y1"];
		$x2 = $post["x2"];
		$y2 = $post["y2"];
		$w = $post["w"];
		$h = $post["h"];
		$scale = $this->thumb_width/$w;
		if(file_exists($this->large_image_location)) {
		$cropped = $this->resizeThumbnailImage($this->thumb_image_location, $this->large_image_location,$w,$h,$x1,$y1,$scale);
		//2) 裁切图片地址
		//$this->thumb_photo_exists = "<img src=\"".gurl("data")."/upload_pic/".$this->thumb_image_name.$_SESSION['user_file_ext']."\" alt=\"裁切区域的图像\"/>";
		$this->thumb_photo_exists = $this->thumb_image_name.$_SESSION['user_file_ext'];
		unset($_SESSION['random_key']);
		}
		return $this->thumb_photo_exists;
	}
	
	//检测上传
	function check_upload($image) {
		if((!empty($image)) && ($image['error'] == 0)) {
			foreach ($this->allowed_image_types as $mime_type => $ext) {
				if($this->file_ext==$ext && $this->userfile_type==$mime_type){
					$error = "";
					break;
				}else{
					$error = "文件 <strong>".$this->image_ext."</strong> 不是期望的格式。<br />";
				}
			}
			if ($this->userfile_size > ($this->max_file*1048576)) {
				$error.= "图片不要超过 ".$this->max_file."MB";
			}
		} else {
			$error= "请先选择图像";
		}
		return $error;
	}
	
	//缩放图片处理
	function resizeImage($image,$width,$height,$scale) {
		list($imagewidth, $imageheight, $imageType) = getimagesize($image);
		$imageType = image_type_to_mime_type($imageType);
		$newImageWidth = ceil($width * $scale);
		$newImageHeight = ceil($height * $scale);
		$newImage = imagecreatetruecolor($newImageWidth,$newImageHeight);
		switch($imageType) {
			case "image/gif":
				$source=imagecreatefromgif($image); 
				break;
			case "image/pjpeg":
			case "image/jpeg":
			case "image/jpg":
				$source=imagecreatefromjpeg($image); 
				break;
			case "image/png":
			case "image/x-png":
				$source=imagecreatefrompng($image); 
				break;
		}
		imagecopyresampled($newImage,$source,0,0,0,0,$newImageWidth,$newImageHeight,$width,$height);
		
		switch($imageType) {
			case "image/gif":
				imagegif($newImage,$image); 
				break;
			case "image/pjpeg":
			case "image/jpeg":
			case "image/jpg":
				imagejpeg($newImage,$image,90); 
				break;
			case "image/png":
			case "image/x-png":
				imagepng($newImage,$image);  
				break;
		}
		
		chmod($image, 0777);
		return $image;
	}
	
	//裁切主函数
	function resizeThumbnailImage($thumb_image_name, $image, $width, $height, $start_width, $start_height, $scale){
		list($imagewidth, $imageheight, $imageType) = getimagesize($image);
		$imageType = image_type_to_mime_type($imageType);
		
		$newImageWidth = ceil($width * $scale);
		$newImageHeight = ceil($height * $scale);
		$newImage = imagecreatetruecolor($newImageWidth,$newImageHeight);
		switch($imageType) {
			case "image/gif":
				$source=imagecreatefromgif($image); 
				break;
			case "image/pjpeg":
			case "image/jpeg":
			case "image/jpg":
				$source=imagecreatefromjpeg($image); 
				break;
			case "image/png":
			case "image/x-png":
				$source=imagecreatefrompng($image); 
				break;
		}
		imagecopyresampled($newImage,$source,0,0,$start_width,$start_height,$newImageWidth,$newImageHeight,$width,$height);
		switch($imageType) {
			case "image/gif":
				imagegif($newImage,$thumb_image_name); 
				break;
			case "image/pjpeg":
			case "image/jpeg":
			case "image/jpg":
				imagejpeg($newImage,$thumb_image_name,90); 
				break;
			case "image/png":
			case "image/x-png":
				imagepng($newImage,$thumb_image_name);  
				break;
		}
		chmod($thumb_image_name, 0777);
		return $thumb_image_name;
	}
	
	function getHeight($image) {
		$size = getimagesize($image);
		$height = $size[1];
		return $height;
	}
	
	function getWidth($image) {
		$size = getimagesize($image);
		$width = $size[0];
		return $width;
	}
	
}
	
?>